package com.atguigu.controller;

import cn.hutool.core.util.IdUtil;
import com.atguigu.utils.PerformanceRunnerResult;
import com.atguigu.utils.PerformanceTest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class TestController {

    @GetMapping("/myTest1")
    public boolean test1() {
        System.out.println(IdUtil.fastSimpleUUID());
        return true;
    }

    //http://localhost:8080/performance/perTest1
    @GetMapping("/performance/perTest1")
    public void performance1() {
        //请求数
        int requests = 1000;

        //并发线程数
        int concurrency = 100;

        //restTemplate  远程调用模板
        RestTemplate restTemplate = new RestTemplate();

        //声明进行测试的业务方法地址
        String url = "http://localhost:8080/myTest1";
        System.out.println("进行测试的地址：" + url);

        //调用压力测试方法（请求数，线程数，要进行压力测试的某个方法
        PerformanceRunnerResult result = PerformanceTest.performanceTest(requests, concurrency, () -> {
            restTemplate.getForObject(url, String.class);
        });

        //打印测试返回值
        System.out.println(result);
    }

}
